import { combineReducers } from 'redux'
function todos(state = [], action) {
  const { type, payload } = action
  switch (type) {
    case 'GET_LIST':
      return payload
    case 'ADD_LIST':
      return [payload, ...state]
    case 'CHANGE_DONE':
      return state.map((item) => {
        if (item.id === payload.id) {
          return {
            ...item,
            done: payload.done,
          }
        } else {
          return item
        }
      })
    case 'DEL_LIST':
      return state.filter((item) => item.id !== payload.id)
    case 'REVISE_LIST':
      return state.map((item) => {
        if (item.id === payload.id) {
          return {
            ...item,
            name: payload.name,
          }
        } else {
          return item
        }
      })
    default:
      return state
  }
}
function type(state = 'all', action) {
  const { type, payload } = action
  if (type === 'FILTER_LIST') {
    return payload.todoType
  }
  return state
}
export default combineReducers({
  todos,
  type,
})
